import { Inject } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { UserProfileHistoryEntity } from '../../entity/history';
import { UserProfileHistoryService } from '../../service/history';
import { UserProfileProfileEntity } from '../../entity/profile';
import { BaseSysUserEntity } from '../../../base/entity/sys/user';

/**
 * 档案修改历史
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: UserProfileHistoryEntity,
  service: UserProfileHistoryService,
  pageQueryOp: {
    fieldEq: ['a.operatorId', 'a.modifiedField'],
    select: ['a.*', 'b.name AS profileName', 'c.name AS operatorName'],
    join: [
      {
        entity: UserProfileProfileEntity,
        alias: 'b',
        condition: 'a.profileId = b.id',
        type: 'innerJoin',
      },
      {
        entity: BaseSysUserEntity,
        alias: 'c',
        condition: 'a.operatorId = c.id',
        type: 'leftJoin',
      },
    ],
    where: ctx => {
      const { operationTime } = ctx.request.body;
      if (operationTime && operationTime.length === 2) {
        return [
          [
            'a.operationTime BETWEEN :start AND :end',
            { start: operationTime[0], end: operationTime[1] },
          ],
        ];
      }
    },
  },
})
export class AdminUserProfileHistoryController extends BaseController {
  @Inject()
  userProfileHistoryService: UserProfileHistoryService;
}
